BEGIN TRANSACTION;
GO

-- 删除外键约束
IF OBJECT_ID('FK_Departments_Users_ManagerId') IS NOT NULL
    ALTER TABLE [Departments] DROP CONSTRAINT [FK_Departments_Users_ManagerId];
GO

IF OBJECT_ID('FK_Departments_Departments_ParentDepartmentId') IS NOT NULL
    ALTER TABLE [Departments] DROP CONSTRAINT [FK_Departments_Departments_ParentDepartmentId];
GO

IF OBJECT_ID('FK_Materials_SecondLevelCategories_SecondLevelCategoryId') IS NOT NULL
    ALTER TABLE [Materials] DROP CONSTRAINT [FK_Materials_SecondLevelCategories_SecondLevelCategoryId];
GO

IF OBJECT_ID('FK_RolePermissions_Roles_RoleId') IS NOT NULL
    ALTER TABLE [RolePermissions] DROP CONSTRAINT [FK_RolePermissions_Roles_RoleId];
GO

IF OBJECT_ID('FK_SecondLevelCategories_FirstLevelCategories_FirstLevelCategoryId') IS NOT NULL
    ALTER TABLE [SecondLevelCategories] DROP CONSTRAINT [FK_SecondLevelCategories_FirstLevelCategories_FirstLevelCategoryId];
GO

IF OBJECT_ID('FK_SupplierContacts_Suppliers_SupplierId') IS NOT NULL
    ALTER TABLE [SupplierContacts] DROP CONSTRAINT [FK_SupplierContacts_Suppliers_SupplierId];
GO

IF OBJECT_ID('FK_SupplierQualifications_Suppliers_SupplierId') IS NOT NULL
    ALTER TABLE [SupplierQualifications] DROP CONSTRAINT [FK_SupplierQualifications_Suppliers_SupplierId];
GO

IF OBJECT_ID('FK_UserRole_Roles_RoleId') IS NOT NULL
    ALTER TABLE [UserRole] DROP CONSTRAINT [FK_UserRole_Roles_RoleId];
GO

IF OBJECT_ID('FK_UserRole_Users_UserId') IS NOT NULL
    ALTER TABLE [UserRole] DROP CONSTRAINT [FK_UserRole_Users_UserId];
GO

IF OBJECT_ID('FK_Users_Departments_DepartmentId') IS NOT NULL
    ALTER TABLE [Users] DROP CONSTRAINT [FK_Users_Departments_DepartmentId];
GO

-- 删除表
DROP TABLE IF EXISTS [Materials];
GO

DROP TABLE IF EXISTS [RolePermissions];
GO

DROP TABLE IF EXISTS [SupplierContacts];
GO

DROP TABLE IF EXISTS [SupplierQualifications];
GO

DROP TABLE IF EXISTS [UserRole];
GO

DROP TABLE IF EXISTS [SecondLevelCategories];
GO

DROP TABLE IF EXISTS [Suppliers];
GO

DROP TABLE IF EXISTS [Roles];
GO

DROP TABLE IF EXISTS [FirstLevelCategories];
GO

DROP TABLE IF EXISTS [Users];
GO

DROP TABLE IF EXISTS [Departments];
GO

-- 删除迁移历史记录
DELETE FROM [__EFMigrationsHistory];
GO

COMMIT;
GO